﻿using GuanYijia.Application.CustomerManagement.CustomerDTO;
using GuanYijia.Application.WarehouseManagement.WarehouseDTO;
using GuanYijia.DoMain;
using GuanYijia.DoMain.PageDto;
using GuanYijia.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GuanYijia.Application.WarehouseManagement
{
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v1")]
    public class WarehouseDataService : IDynamicWebApi
    {
        private readonly IBaseRepository<Warehouse> _warehouseRepository;

        private readonly IBaseRepository<Head> _headRepository;

        public WarehouseDataService(IBaseRepository<Warehouse> warehouseRepository, IBaseRepository<Head> headRepository)
        {
            _warehouseRepository = warehouseRepository;
            _headRepository = headRepository;
        }
        /// <summary>
        /// 供应商下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Head>> GetHeadInfo()
        {
            var redisKey = "GetHeadInfo_Key";
            var HeadInfoShowValue = RedisHelper.Get<List<Head>>(redisKey);
            if(HeadInfoShowValue==null|| !HeadInfoShowValue.Any())
            {
                HeadInfoShowValue= await _headRepository.GetAllAsync();
                var hours = new Random().Next(10,10);
                RedisHelper.Set(redisKey, HeadInfoShowValue, TimeSpan.FromHours(hours));
            }
            return await _headRepository.GetAllAsync();
        }
        [HttpPost]
        public async Task<PageResultDto<WarehouseRequisitionOutPut>> GetWarehouseShow(WarehouseRequisitionInPut warehouseinput)
        {
            var list = new PageResultDto<WarehouseRequisitionOutPut>() { TotalCount = 0, TotalPage = 0, Data = new List<WarehouseRequisitionOutPut>() };
            var res = from a in  _warehouseRepository.GetQueryable(tracking:false)
                      join b in  _headRepository.GetQueryable(tracking: false) on a.principal_id equals b.principal_id
                      select new WarehouseRequisitionOutPut
                      {
                          depot_id = a.depot_id,
                          depot_name = a.depot_name,
                          depot_address = a.depot_address,
                          warehousing = a.warehousing,
                          truckage = a.truckage,
                          sort = a.sort,
                          principal_id = a.principal_id,
                          principal_name = b.principal_name,
                          remark = a.remark,
                          enabled = a.enabled,
                          is_default = a.is_default
                      };
            if (!string.IsNullOrEmpty(warehouseinput.depot_name))
            {
                res = res.Where(x => x.depot_name.Contains(warehouseinput.depot_name));
            }
            if (!string.IsNullOrEmpty(warehouseinput.remark))
            {
                res = res.Where(x => x.remark.Contains(warehouseinput.remark));
            }
            list.TotalCount = res.Count();
            list.TotalPage = (int)Math.Ceiling(list.TotalCount * 1.0 / warehouseinput.pageSize);
            list.Data = await res.OrderBy(x => x.sort).Skip((warehouseinput.pageIndex - 1) * warehouseinput.pageSize).Take(warehouseinput.pageSize).ToListAsync();
            return list;
        }
        /// <summary>
        /// 逻辑单删
        /// </summary>
        /// <param name="supplierId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> DeleteWarehouse(int depot_id)
        {
            var requisition = await _warehouseRepository.GetAsync(depot_id);
            requisition.delete_Flag = true;
            return await _warehouseRepository.UpdateAsync(requisition);
        }
        /// <summary>
        /// 逻辑批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> RangeDeleteWarehouse(List<int> ids)
        {
            var requisitionList = await _warehouseRepository.GetAllAsyncList(x => ids.Contains(x.depot_id));
            foreach (var item in requisitionList)
            {
                item.delete_Flag = true;
            }
            return await _warehouseRepository.UpdateRangeAsync(requisitionList);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddWarehouse(Warehouse warehouse)
        {
            var list = _warehouseRepository.GetQueryable();
            if (list.Where(x => x.depot_name == warehouse.depot_name).Count() > 0)
            {
                return -1;
            }
            return await _warehouseRepository.InsertAsync(warehouse);
        }
        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateWarehouseCanenable(List<int> ids)
        {
            var requisitionList = await _warehouseRepository.GetAllAsyncList(x => ids.Contains(x.depot_id));
            foreach (var item in requisitionList)
            {
                item.enabled = false;
            }
            return await _warehouseRepository.UpdateRangeAsync(requisitionList);
        }
        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateWarehouseDisable(List<int> ids)
        {
            var requisitionList = await _warehouseRepository.GetAllAsyncList(x => ids.Contains(x.depot_id));
            foreach (var item in requisitionList)
            {
                item.enabled = true;
            }
            return await _warehouseRepository.UpdateRangeAsync(requisitionList);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="depot_id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Warehouse> GetWarehouseInfo(int depot_id)
        {
            return await _warehouseRepository.GetAsync(depot_id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateWarehouse(Warehouse warehouse)
        {
            var list = _warehouseRepository.GetQueryable(tracking: false);
            if (list.Where(x => x.depot_name == warehouse.depot_name).Count() > 0)
            {
                return -1;
            }
            return await _warehouseRepository.UpdateAsync(warehouse);
        }
    }
}
